package cn.wolfcode.web.controller;
import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.ClassinfoMapper;
import cn.wolfcode.qo.ClassinfoQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IClassinfoService;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {
    @Autowired
    private IClassinfoService classinfoService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private ClassinfoMapper classinfoMapper;
    @Autowired
    private IEmployeeService employeeService;

    @RequiredPermission(name = "班级列表",expression = "classinfo:list")
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") ClassinfoQueryObject qo) {
        PageInfo<Classinfo> pageResult = classinfoService.query(qo);
        model.addAttribute("pageInfo", pageResult);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        List<Classinfo> classinfos = classinfoService.listAll();
        HashSet<Long> ids = new HashSet<>();
        for (Classinfo classinfo:classinfos){
            if (classinfo.getEmployee().getId()!=null){
                ids.add(classinfo.getEmployee().getId());
            }
        }
        List<Employee> list = employeeService.selectByIds(ids);

        model.addAttribute("list",list);
        return "classinfo/list";
    }
    @RequiredPermission(name = "班级删除",expression = "classinfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        if(id !=null){
            classinfoService.delete(id);
        }
        return new JsonResult();
    }
    /*@RequiredPermission(name = "班级录入",expression = "classinfo:input")
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if (id !=null){
            Classinfo classinfo = classinfoService.get(id);
            model.addAttribute("classinfo",classinfo);
        }
        return "classinfo/input";
    }*/
    @RequiredPermission(name = "班级保存或编辑",expression = "classinfo:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo){
        if(classinfo.getId() !=null){
            classinfoService.update(classinfo);
        }else{
            classinfoService.save(classinfo);
        }
        return new JsonResult(true,"保存成功");
    }
}
